package com.eascs.web.o2o.web.reward.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.eascs.app.dao.PageResult;
import com.eascs.app.dao.SqlPageQuery;
import com.eascs.app.log.Logger;
import com.eascs.app.log.LoggerFactory;
import com.eascs.site.view.BaseRestResult;
import com.eascs.web.o2o.web.reward.dao.BaseDao;
import com.eascs.web.o2o.web.reward.entity.CmRdRelation;
import com.eascs.web.o2o.web.reward.entity.LoanDetailForm;
import com.eascs.web.o2o.web.reward.service.CmRdRelationService;
import com.eascs.web.sso.User;
import com.eascs.web.sso.UserHolder;

@Service
public class CmRdRelationServiceImpl implements CmRdRelationService{
	
	private static Logger log = LoggerFactory.getLogger(OverdualReportServiceImpl.class);
	
	@Autowired BaseDao baseDao;

	@Override
	public PageResult<Object[]> getCmRdRelationList(LoanDetailForm entity, String type) {
		SqlPageQuery<Object[]> sqlPageQuery = new SqlPageQuery<>(Object[].class, entity.getPageSize(),
				entity.getShowPage());
		StringBuffer querySql = new StringBuffer();
		if("search".equals(type)) {
			querySql.append("select t_month,province_name,cm_code,"
					+ "cm_name,rd_code,rd_name "
					+ "from dim_reward_cm_rd_relation "
					+ "where /**DATA_PERMISSION(ltd)**/ 1=1");
			sqlPageQuery.setQuerySql(querySql.toString());
			if(entity.getCheckMonth()!=null) {
				sqlPageQuery.setQuerySql(sqlPageQuery.getQuerySql()+" and t_month=?");
				sqlPageQuery.addParam(entity.getCheckMonth());
			}
			if(entity.getProvinceName()!=null) {
				sqlPageQuery.setQuerySql(sqlPageQuery.getQuerySql()+" and province_name=?");
				sqlPageQuery.addParam(entity.getProvinceName());
			}
			sqlPageQuery.setQuerySql(sqlPageQuery.getQuerySql()+" order by t_month DESC");
		} else if("download".equals(type)) {
			querySql.append("select t_month,province_code,province_name,"
					+ "cm_no,cm_code,cm_name,rd_no,rd_code,rd_name,"
					+ "ltd,ltd_code,ltd_name "
					+ "from dim_reward_cm_rd_relation "
					+ "where t_month = date_format(date_sub(now(),interval 1 month),'%Y-%m')");
			sqlPageQuery.setQuerySql(querySql.toString());
		}
		System.out.println(sqlPageQuery.getQuerySql());
		return baseDao.findDataWithPage(sqlPageQuery);
	}

	@Override
	@Transactional("transactionManagerDbForReward")
	public BaseRestResult<String> addCmRdRelationList(List<List<Object>> result) {
		BaseRestResult<String> results = BaseRestResult.info("导入成功").andRefresh();
		User user=UserHolder.getUser();
		baseDao.executeSql(" delete from dim_reward_cm_rd_relation where  t_month = DATE_FORMAT(DATE_SUB(now(),INTERVAL 1 month),'%Y-%m')");
		//将excel中的数据更新到数据库中
		if(result!=null&&result.size()>0){
			for (int i = 0; i < result.size(); i++) {
				@SuppressWarnings({ "unchecked", "rawtypes" })
				List<Object> list = (List) result.get(i);
				CmRdRelation cmRdRelation = new CmRdRelation();
				cmRdRelation.setT_month(list.get(0).toString());
				cmRdRelation.setProvince_code(list.get(1).toString());
				cmRdRelation.setProvince_name(list.get(2).toString());
				cmRdRelation.setCm_no(list.get(3).toString());
				cmRdRelation.setCm_code(list.get(4).toString());
				cmRdRelation.setCm_name(list.get(5).toString());
				cmRdRelation.setRd_no(list.get(6).toString());
				cmRdRelation.setRd_code(list.get(7).toString());
				cmRdRelation.setRd_name(list.get(8).toString());
				cmRdRelation.setLtd(list.get(9).toString());
				cmRdRelation.setLtd_code(list.get(10).toString());
				cmRdRelation.setLtd_name(list.get(11).toString());
				cmRdRelation.setIs_lock("N");
				cmRdRelation.setCreate_user(user.getCname());
				cmRdRelation.setCreate_date(new Date());
				cmRdRelation.setUpdate_user(user.getCname());
				cmRdRelation.setUpdate_date(new Date());
				cmRdRelation.setConfirm_user(user.getCname());
				cmRdRelation.setConfirm_date(new Date());
				baseDao.saveEntity(cmRdRelation);
			}
		}else{
			log.error("导入的数据为空，事务回滚");
			throw new RuntimeException("导入的数据为空，请重新导入！");
		}
	   return results;
	}

}
